<?
class clsSearch extends yellowpages{
 function sql(){
  $a = func_get_args();
  if(isset($a[4])){
   if($a[4]>1) 
    $a[4]=($a[4]-1)*10; 
   else
    $a[4]=0;
  }
  $w=""; $chr=""; $tw='';
  if(isset($_REQUEST['chr'])){ $w="and company_$a[2] like '".$_REQUEST['chr']."%'";}
  if(isset($_REQUEST['tw'])) $tw = "and town.town_id=".$_REQUEST['tw'];
  $o="order by ad DESC, company_$a[1]";
  $s = " SELECT company.company_id, company_$a[1], company_add_$a[1], town_name_$a[1], tel_fax, 
		 prefix, email_address, internet_address, m_phone_no, mdphoneno, map, book, ad, 
		 ad1_image, ad1_link_$a[1], imgfile ";
  switch($a[0]){
   case "search":
    $sql="$s
	     FROM company, company_name, no_tel, town 
		 WHERE (company.company_id=company_name.idall) and (company.company_id=no_tel.idall) 
		 and (company.town_id=town.town_id)
		 and (company_$a[2] like '%$a[3]%') $tw"; 
	$_SESSION['sqlTown']="select company_name.town_id, town_name_$a[1] from company_name, town 
						where (company_name.town_id=town.town_id) and (company_$a[2] like '%$a[3]%')
						group by town_name_$a[1]
						order by town_name_$a[1]";
	$_SESSION['sqlnum']="$sql $w "; 
	$_SESSION['sqlchr'] = "$sql $o";
	$sql.="$w $o limit $a[4],10";
	break;
	
   case "tel":
    $sql="$s
	     FROM company, company_name, no_tel, town 
		 WHERE (company.company_id=company_name.idall) and (company.company_id=no_tel.idall) 
		 and (company.town_id=town.town_id)
		 and (tel_fax like '%$a[3]%') $tw"; 
	$_SESSION['sqlTown']="select no_tel.town_id, town_name_$a[1] from no_tel, town 
						where (no_tel.town_id=town.town_id) and (tel_fax like '%$a[3]%')
						group by town_name_$a[1]
						order by town_name_$a[1]";
	$_SESSION['sqlnum']="$sql $w "; 
	$_SESSION['sqlchr'] = "$sql $o";
	$sql.="$w $o limit $a[4],10";
	break;
	
   case "comform"://    print_r($a);
    if(isset($_REQUEST['tw'])) $tw = "and town.town_id=".$_REQUEST['tw'];
    $sql="$s
		 FROM company, company_name, no_tel, town 
		 WHERE (company.company_id=company_name.idall) and (company.company_id=no_tel.idall) 
		 and (company.town_id=town.town_id)	and (com_form_id=$a[3]) $tw "; 
	$_SESSION['sqlTown']="select company.town_id,town_name_$a[1] from company, town 
						where (company.town_id=town.town_id) and (com_form_id=$a[3]) $tw
						group by town_name_$a[1]
						order by town_name_$a[1]";
	$_SESSION['sqlnum']="$sql $w "; 
	$_SESSION['sqlchr'] = "$sql $o";
	$sql.="$w $o limit $a[4],10";
	break;
   
   case 'trade':
    if(isset($_REQUEST['tw'])) $tw = "and town.town_id=".$_REQUEST['tw'];
    $sql="$s 
		FROM company, company_name, no_tel, town, id_trade
		WHERE (company.company_id=company_name.idall) and (company.company_id=no_tel.idall) 
		and (company.town_id=town.town_id) and (company.company_id=id_trade.company_id) 
		and (trade_name_1=$a[3]) $tw ";
	$_SESSION['sqlTown']="select id_trade.town_id,town_name_$a[1] from town, id_trade 
						where (id_trade.town_id=town.town_id) and (trade_name_1=$a[3])
						group by town_name_$a[1]
						order by town_name_$a[1]";
	$_SESSION['sqlnum']="$sql $w "; 
	$_SESSION['sqlchr'] = "$sql $o";
	$sql.="$w $o limit $a[4],10";
    break;
   case "productList":
    if(isset($_REQUEST['tw'])) $tw = "and town.town_id=".$_REQUEST['tw'];
    $sql="$s 
		FROM company, company_name, no_tel, town, id_class
		WHERE (company.company_id=company_name.idall) and (company.company_id=no_tel.idall) 
		and (company.town_id=town.town_id) and (company.company_id=id_class.company_id) 
		and (class_id_1=$a[3]) $tw ";
	$_SESSION['sqlTown']="select id_class.town_id,town_name_$a[1] from town, id_class 
						where (id_class.town_id=town.town_id) and (class_id_1=$a[3])
						group by town_name_$a[1]
						order by town_name_$a[1]";
	$_SESSION['sqlnum']="$sql $w "; 
	$_SESSION['sqlchr'] = "$sql $o";
	$sql.="$w $o limit $a[4],10";	 
	break;
   case "head":
    $sql = "select head_$a[2], infone_$a[2], inftwo_$a[2],link_$a[2] from vb_memberprofile, vb_custad_en 
			where vb_memberprofile.memID = vb_custad_en.memID and id=$a[1]";
    break;
  }
  return $sql;
 }
 
 function showResult(){//$_SESSION['istype'], $lang, $_SESSION['lng'], $key, $pg, $ids, $tw, $link
  $a = func_get_args(); $i=0; $j=0;$k=0;//print_r($a);
  $sql = $this->sql($a[0],$a[1],$a[2],$a[3],$a[4]);// echo("$sql<br>");
  if($a[4]>1) $a[4]=$a[4]*10;
  $result = mysql_query($sql) or die("counld not query");
  $t = "";
  while($rs=mysql_fetch_array($result,MYSQL_NUM)){
   $str=""; $adv=""; $k++;
   $rs[12]=(int)$rs[12];// echo(var_dump($rs[12])."<br>");
   if(($rs[12]>0)&&($i==0)){ 
    $t.= "<tr class=bgCom>
		 <td colspan=2 style=padding-left:3px height=20><b>Yellow Pages - Advertisers</b></td>
		 <td align=right style=padding-right:3px>* Print</td></tr>";
	$i++; $j++;
	$adv="<a class=lChar href='moredetail$a[7].php?ids=$a[5]$a[6]'>business profile</a>";
   }
   
   if(($rs[12]==0)&&($i>0)){ //echo(var_dump($rs[12]));
    $t = substr($t,0,strlen($t)-28);//."<tr><td>&nbsp;</td></tr>";
    $t.= "<tr class=bgCom><td colspan=2 style=padding-left:3px height=20><b>Business Listings</b></td>
			  <td align=right style=padding-right:3px>* Print</td></tr>";
	$i=0; $j++;
   }
   if($rs[13]) $str="<img class=mrlogo border=0 src=$rs[13]>";
   if($rs[14]) $str="<a href=$rs[14]>$str</a>";
   if($str) $str.="<br>";
   $str.= str_replace("|","",$rs[2])." $rs[3] <br>";
   $tel=explode("|",$rs[4]); //print_r($tel);
   $str.="Tel: (84-$rs[5]) $tel[0]<br>";
   if(isset($tel[1])) $str.="Fax: (84-$rs[5]) $tel[1]<br>";
   
   if($rs[6]) 
    $rs[6]=" | <a class=lChar href='feedback$a[7].php?ids=$a[5]&url=$rs[6]&kw=".base64_encode($rs[1])."'>email this business</a>";
   else
    $rs[6]=" | <a class=lChar href='feedback$a[7].php?ids=$a[5]&url=&kw=".base64_encode($rs[1])."'>email this business</a>";
	
   if($rs[7]) $rs[7] = " | <a class=lChar href='http://$rs[7]' title='$rs[7]' target=_black>website</a>";
   $adv = $adv.$rs[6].$rs[7];
   if(strpos($adv,"|")==1) $adv=substr(trim($adv),2);
   $str.="<font id=pro><script language=javascript></script></font>$adv";
   $t.="<tr><td class=pdr5 height=22>$a[4]</td>
   		<td><a class=".$this->clsAd($rs[12])." href=moredetail$a[7].php?ids=$a[5]$a[6]><b>$rs[1]</b></a></td>
   		<td align=right><input name=chk$k type=checkbox value=$rs[0]></td></tr>
		<tr><td></td><td>$str</td><td align=right>".$this->getHead($rs[0],$a[1])."</td></tr>
		<tr><td>&nbsp;</td><td colspan=2><hr></td></tr>";
   $a[4]++;
  }
  if($i==0&&$j==0) 
   $t = "<tr class=bgCom><td colspan=2 style=padding-left:3px height=20><b>Business Listings</b></td>
	     <td align=right style=padding-right:3px>* Print</td></tr>$t";
  return "<table class=tab cellpadding=0 cellspacing=0 width=100%>".$t."</table>";
 }
 
 function getHead($id,$lang){
  //echo($this->sql('head',$id,$lang));
  $result = mysql_query($this->sql('head',$id,$lang)) or die('counld not query');
  $rs = mysql_fetch_array($result,MYSQL_NUM);
  $t='';
  if($rs){
   if($rs[1]) $rs[1]="$rs[1]<br>";
   if($rs[2]) $rs[2]="$rs[2]<br>";
   $t="<table class=tab style='border:#d9d9a4 1px solid' cellpadding=0 cellspacing=0 width=183>
       <tr><td>$rs[0]<br>$rs[1]$rs[2]<a class=head href='http://$rs[3]'>$rs[3]</a></td></tr>
       </table>";
  }
  return $t;
 }
 
 //$_SESSION['sql'],$lang,$_SESSION['lng'],$key,$pg,$lnk,$ids
 function getNum(){//$_SESSION['sql'],$lang,$_SESSION['lng'],$key,$pg, $limit, $lnk, ids
  $a		= func_get_args(); $p=array(); $chrlnk=""; $tw='';
  if(isset($_REQUEST['chr'])) $chrlnk="&chr=".$_REQUEST['chr'];
  if(isset($_REQUEST['tw'])) $tw=$_REQUEST['tw'];
  $lnglnk=$a[1]; $prev="";
  if($a[1]=='us') $lnglnk='';
  $url		= "$a[8]$lnglnk.php?ids=$a[7]";
  $sql		= $a[0];// echo("<br>$a[0]<br>");
  $result	= mysql_query($sql) or die('counld not query num rows');
  $num 		= mysql_num_rows($result);
  mysql_free_result($result);
  
  $result	= mysql_query($_SESSION['sqlchr']) or die('counld not query num rows');
  $alpha	= $this->getAlpha($result,$url);
  mysql_free_result($result);
  $lg=$this->getName($a[1],$url);
  $pgNum = $num/$a[5];
  if(is_float($pgNum)) $pgNum = floor($pgNum)+1;
  
  if($a[4]==1){
   $bg = 0; $end = $a[5]; $next = "<img border=0 src=images/next.gif>"; 
   $lnk="<a class=lnext href=$url&pg=2$chrlnk>$lg[1]</a>";
   $numend=$a[4]*$a[5];
   if($pgNum>10) $pgNum=$numend;
   if($num<=10){ $numend=$num; $next=""; $lnk="";}
   $p[0] = "<table class=tab cellpadding=0 cellspacing=0 width=100%>
   		   <tr><td><img src=images/next.gif> $lg[5] $a[6] </td>
   		   <td align=right>$lg[2] 1 - $numend $lg[3] $num $next $lnk</td></tr></table>";
   $p[1] = "<table class=tab cellpadding=0 cellspacing=0 width=100%><tr>
   			<tr><td><img src=images/next.gif> $lg[6] </td>
			<td align=right>$lg[4] ".$this->getPage($bg,$pgNum,$a[8],$lnglnk,$a[7],$a[4],$chrlnk)."
			<a href=$url&pg=".(floor($a[4]/$a[5])+$a[5]).">$next</a></td></tr>
			</table>";
  }
  
  if(($a[4]>1)&&($a[4]<=$pgNum)){
   $pg = $a[4]/$a[5]; $bg = 0; $end = $a[5]; 
   if($pg>1){
    if(is_int($pg)) $pg = $pg-1;
    $tmp=floor($pg)*$a[5];
    $bg=$tmp; $end=$tmp+$a[5]; 
   }
   $numend=($a[4]+1)*$a[5];
   $next="<a href=$url&pg=".($end+1)."$chrlnk><img border=0 src=images/next.gif></a>";
   if($a[4]>$a[5]){
    $prev = "<a href=$url&pg=$tmp$chrlnk><img border=0 src=images/back.gif></a>";
	$next = "<a href=$url&pg=".($end+1)."$chrlnk><img border=0 src=images/next.gif></a>";
	$numend = ($a[4]+1)*$a[5];
   }
   if($a[4]==($pgNum-1)){
    $prev = "<a href=$url&pg=$tmp$chrlnk><img border=0 src=images/back.gif></a>";
	$next="";
   }
   $p[0] = "<table class=tab cellpadding=0 cellspacing=0 width=100%><tr><td><img src=images/next.gif> $lg[5] $a[6] </td>
   		   <td align=right><a class=lnext href=$url&pg=".($a[4]-1)."$chrlnk>$lg[0] </a> 
		   <img src=images/back.gif> $lg[2] ".($a[4]*$a[5]+1)." - $numend $lg[3] $num <img src=images/next.gif> 
		   <a class=lnext href=$url&pg=".($a[4]+1)."$chrlnk>$lg[1]</a></td></tr></table>";
   $p[1] = "<table class=tab cellpadding=0 cellspacing=0 width=100%><tr>
   			<tr><td><img src=images/next.gif> $lg[6] </td>
			<td align=right>$prev $lg[4] ".$this->getPage($bg,$end,$a[8],$lnglnk,$a[7],$a[4],$chrlnk)." $next</td></tr>
			</table>";
  }
  $chr="";
  for($i=0;$i<(count($alpha)-1);$i++){
   $chr.=" $alpha[$i]";
  }
   $chr="<table class=tab cellpadding=0 cellspacing=0 width=100%><tr>
   		<td><img src=images/next.gif> $lg[7] ".$alpha[(count($alpha)-1)].$chr."</td>
		<td align=right>".$this->getTown($lg[8],$tw)."</td></tr></table>";
  return "$p[0]||$p[1]||$chr||$num";
 }
 
 function getAlpha(){
  $a = func_get_args();
  $t = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", 
  			  "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
  $flag=0; $k=0;
  while($rs=mysql_fetch_array($a[0])){
   $rs[1] = substr($rs[1],0,1); 
   if((is_numeric($rs[1]))&&($k==0)){ $t[]="<a class=nopage href=$a[1]>0-9</a>"; $k++;}
   for($i=0; $i<count($t); $i++)
    if($rs[1]==$t[$i]) $t[$i] = "<a class=nopage href=$a[1]&chr=$t[$i]>$t[$i]</a>";
  }
  return $t;
 }
 
 function getPage(){// $bg,$end,$a[8],$lnglnk,$a[7],$a[4],$chrlnk
  $a = func_get_args(); //print_r($a);
  $t='';
  for($i=$a[0];$i<$a[1];$i++){
   $tmp=$i+1;
   if($tmp==$a[5])
    $t.=" $tmp ";
   else
    $t.="<a class=nopage href=$a[2]$a[3].php?pg=$tmp&ids=$a[4]$a[6]>$tmp</a> ";
  }
  return $t;
 }
 
 function getName(){
  $a=func_get_args(); $t=array();
  switch($a[0]){
   case 'us':
    $t[]='Prev'; $t[]='Next'; $t[]='Listings'; $t[]='of'; $t[]='Page'; $t[]='Language:'; 
	$t[]="Sort: <a class=nopage href=$a[1]>Alphabetically</a>"; $t[]='Jump to:'; $t[]='Sort by Province';
	break;
   case 'vn':
    $t[]='Trước'; $t[]='Tiếp'; $t[]='Hiển Thị'; $t[]='trong'; $t[]='Trang'; $t[]='Ngôn ngữ';
	$t[]="Sort: <a class=nopage href=$a[1]>Tất cả</a>"; $t[]='Lọc theo tên:'; $t[]='Tìm Theo Khu Vực';
	break;
   case 'cn':
    $t[]='预演'; $t[]='未来'; $t[]='上市'; $t[]=' , '; $t[]='页'; $t[]='语言';
	$t[]="Sort: <a class=nopage href=$a[1]>阿尔法</a>"; $t[]='跳到:'; $t[]='各省类';
	break;
  }
  return $t;
 }
 
 function clsAd($a){
  switch($a){
   case 9:
    $t = "colCom"; break;
   case 2:
    $t = "colCom1"; break;
   case 1:
    $t = "colCom2"; break;
   default:
    $t = "colCom3";
  }
  return $t;
 }
 
 function leftMenu(){
  $a = func_get_args();
  if($a[2])
   $t="<tr><td></td></tr>";
  else
   $t="<tr><td class=pdt10 align=center><img src=images/img1.gif></td><tr>
       <tr><td class=pdtb10 align=center><img src=images/img2.gif></td><tr>";
  if($a[0])
   $t = $a[0]."<tr><td align=center height=22 valign=top><img id=imgdown onMouseOver=\"upAndDown('ismenu','down','imgup','imgdown')\" onMouseOut=\"upAndDown('ismenu','out')\" class=hand hspace=5 src=images/bt_downdis.gif><img id=imgup class=hand onMouseOver=\"upAndDown('ismenu','up','imgdown','imgup')\" onMouseOut=\"upAndDown('ismenu','out')\" src=images/bt_up.gif></td></tr>".$a[1].$t;
  else
   $t = $a[1].$t;
  return $t;
 }
 
 function getTown(){//$lang, 
  $a = func_get_args();
  $sql=$_SESSION['sqlTown'];
  $result = mysql_query($sql) or die("counld not query");
  $t="<select name=sltTown class=tab onchange=\"goPages(this.options[this.selectedIndex].value)\"><option value=''>$a[0]</option>";
  while($rs = mysql_fetch_array($result,MYSQL_NUM)){
   $t.="<option value=$rs[0] ".yellowpages::isCheck($rs[0],$a[1]).">$rs[1]</option>";
  }
  return $t.'</select>';
 }
 
 function getLink(){//$istype, $lang, $ids, $_SESSION['word'], $_SESSION['keyword']
  $a = func_get_args(); 
  $t='';
  if($a[1]=='us') $a[1]='';
  switch($a[0]){
   case 'trade':
    $t= "<a class=toplnk href=tradesearch$a[1].php?ids=$a[2]>".base64_decode($a[3])."</a>"; break;
   case 'productList':
    $t = "<a class=toplnk href='product$a[1].php?ids=$a[2]'>".base64_decode($a[3])."</a>"; break;
   case 'search':
    $t = "<a class=toplnk href=product$a[1].php?ids=$a[2]>$a[4]</a>"; break;
   case 'comform':
    $t = "<a class=toplnk href=comform_town$a[1].php?ids=$a[2]&id=$a[4]&word=$a[3]>".base64_decode($a[3])."</a>"; break;
  }
  return $t;
 }
}//end class
?>